<template>
  <div class="single-q">
    <div class="content">
      <n-thing content-indented>
        <template #header v-if="formData.title">
          <n-row class="title">
            <div class="index" v-if="props.index">{{ props.index + '.' }}</div>
            <span v-show="formData.required === '必填'" style="color: red"
              >*</span
            >
            {{ formData.title }}
            <span
              v-if="formData.score > 0"
              style="font-size: 0.8rem; color: #d83701; margin-left: 1px"
            >
              {{ '(' + formData.score + '分)' }}
            </span>
          </n-row>
        </template>
        <template #description v-if="formData.remark">
          <div class="remark">
            {{ '——' + formData.remark }}
          </div>
        </template>
        <n-space vertical>
          <n-radio-group
            name="options"
            v-model:value="formData.option"
            @update:value="upO"
          >
            <n-space vertical>
              <n-radio
                v-for="option in formData.options"
                :key="option"
                :value="option"
              >
                {{ option }}
              </n-radio>
            </n-space>
          </n-radio-group>
          <n-button
            text
            @click="clear"
            v-if="formData.option"
            style="color: #999; font-size: 0.4rem"
            >清除当前选择</n-button
          >
        </n-space>
      </n-thing>
    </div>
  </div>
</template>

<script setup lang="ts">
import { RadioButtonOn as FlashIcon } from '@vicons/ionicons5';
import { ref, reactive } from 'vue';

const props = defineProps({
  index: {
    type: Number,
    default: 0,
  },
  title: {
    type: String,
    required: true,
  },
  required: {
    type: String,
    default: '非必填',
  },
  remark: {
    type: String,
    default: '',
  },
  score: {
    type: Number,
    default: 0,
  },
  options: {
    type: Array,
    required: true,
  },
  option: {
    type: String,
    required: true,
    default: '',
  },
});

const formData: any = reactive({
  index: props.index,
  title: props.title,
  remark: props.remark,
  required: props.required,
  score: props.score,
  options: props.options,
  option: props.option,
});

const emit = defineEmits(['update:option']);
const upO = (value: string) => {
  emit('update:option', value);
};
const clear = () => {
  formData.option = '';
  emit('update:option', formData.option);
};

const formRef = ref(null);
</script>

<style scoped lang="scss">
.single-q {
  .index {
    margin-right: 1vw;
  }
  .content {
    padding: 1vw;
  }
  .title {
    font-size: 1.25rem;
  }
  .remark {
    font-size: 1rem;
    color: #999;
  }
}
</style>
